* Table C6: Socioeconomic status, academic achievement, and ROL characteristics (2)

********************************************************************************
************************** Prepare data ****************************************
********************************************************************************

clear all
use if rank == 1 using "${data_rols}", clear
merge 1:1 id using "${data_students}", keepusing(GPA11_3_std disadv)

gen GPA11_3_std_m = GPA11_3_std == .
label variable GPA11_3_std_m "11th-grade GPA (missing)"

replace GPA11_3_std = 0 if GPA11_3_std_m == 1


********************************************************************************
************************** Prepare table ***************************************
********************************************************************************

** Column 1 ----------------------------------------------------------------
qui reg mp disadv, r
local i = 1

	local est_disadv_`i': 	display %4.3f _b[disadv]
	local se_disadv_`i': 		display %4.3f _se[disadv]
	local p_disadv_`i'_long: 	display (2 * ttail(e(df_r), abs(_b[disadv]/_se[disadv])))
	local r2_`i': display %4.3f e(r2)
	
	if 		`p_disadv_`i'_long'<=0.01	local stars_disadv_`i' "$^{***}$"
	else if `p_disadv_`i'_long'<=0.05	local stars_disadv_`i' "$^{**}$"
	else if `p_disadv_`i'_long'<=0.1	local stars_disadv_`i' "$^{*}$"
	else 						local stars_disadv_`i' ""

** Column 2 ----------------------------------------------------------------	
qui reg mp GPA11_3_std GPA11_3_std_m, r
local i = 2

	local est_gpa_`i': 	display %4.3f _b[GPA11_3_std]
	local se_gpa_`i': 		display %4.3f _se[GPA11_3_std]
	local p_gpa_`i'_long: 	display (2 * ttail(e(df_r), abs(_b[GPA11_3_std]/_se[GPA11_3_std])))
	local r2_`i': display %4.3f e(r2)
	
	if 		`p_gpa_`i'_long'<=0.01	local stars_gpa_`i' "$^{***}$"
	else if `p_gpa_`i'_long'<=0.05	local stars_gpa_`i' "$^{**}$"
	else if `p_gpa_`i'_long'<=0.1	local stars_gpa_`i' "$^{*}$"
	else 						local stars_gpa_`i' ""

** Column 3 ----------------------------------------------------------------
qui reg mp disadv GPA11_3_std GPA11_3_std_m, r
local i = 3

	local est_disadv_`i': 	display %4.3f _b[disadv]
	local se_disadv_`i': 		display %4.3f _se[disadv]
	local p_disadv_`i'_long: 	display (2 * ttail(e(df_r), abs(_b[disadv]/_se[disadv])))
	local r2_`i': display %4.3f e(r2)
	
	if 		`p_disadv_`i'_long'<=0.01	local stars_disadv_`i' "$^{***}$"
	else if `p_disadv_`i'_long'<=0.05	local stars_disadv_`i' "$^{**}$"
	else if `p_disadv_`i'_long'<=0.1	local stars_disadv_`i' "$^{*}$"
	else 						    local stars_disadv_`i' ""
	
	local est_gpa_`i': 	display %4.3f _b[GPA11_3_std]
	local se_gpa_`i': 		display %4.3f _se[GPA11_3_std]
	local p_gpa_`i'_long: 	display (2 * ttail(e(df_r), abs(_b[GPA11_3_std]/_se[GPA11_3_std])))
	
	if 		`p_gpa_`i'_long'<=0.01	local stars_gpa_`i' "$^{***}$"
	else if `p_gpa_`i'_long'<=0.05	local stars_gpa_`i' "$^{**}$"
	else if `p_gpa_`i'_long'<=0.1	local stars_gpa_`i' "$^{*}$"
	else 						local stars_gpa_`i' ""	

** Column 4 ----------------------------------------------------------------
qui reg mp1 disadv, r
local i = 4

	local est_disadv_`i': 	display %4.3f _b[disadv]
	local se_disadv_`i': 		display %4.3f _se[disadv]
	local p_disadv_`i'_long: 	display (2 * ttail(e(df_r), abs(_b[disadv]/_se[disadv])))
	local r2_`i': display %4.3f e(r2)
	
	if 		`p_disadv_`i'_long'<=0.01	local stars_disadv_`i' "$^{***}$"
	else if `p_disadv_`i'_long'<=0.05	local stars_disadv_`i' "$^{**}$"
	else if `p_disadv_`i'_long'<=0.1	local stars_disadv_`i' "$^{*}$"
	else 						local stars_disadv_`i' ""

** Column 5 ----------------------------------------------------------------	
qui reg mp1 GPA11_3_std GPA11_3_std_m, r
local i = 5

	local est_gpa_`i': 	display %4.3f _b[GPA11_3_std]
	local se_gpa_`i': 		display %4.3f _se[GPA11_3_std]
	local p_gpa_`i'_long: 	display (2 * ttail(e(df_r), abs(_b[GPA11_3_std]/_se[GPA11_3_std])))
	local r2_`i': display %4.3f e(r2)
	
	if 		`p_gpa_`i'_long'<=0.01	local stars_gpa_`i' "$^{***}$"
	else if `p_gpa_`i'_long'<=0.05	local stars_gpa_`i' "$^{**}$"
	else if `p_gpa_`i'_long'<=0.1	local stars_gpa_`i' "$^{*}$"
	else 						local stars_gpa_`i' ""

** Column 6 ----------------------------------------------------------------
qui reg mp1 disadv GPA11_3_std GPA11_3_std_m, r
local i = 6

	local est_disadv_`i': 	display %4.3f _b[disadv]
	local se_disadv_`i': 		display %4.3f _se[disadv]
	local p_disadv_`i'_long: 	display (2 * ttail(e(df_r), abs(_b[disadv]/_se[disadv])))
	local r2_`i': display %4.3f e(r2)
	
	if 		`p_disadv_`i'_long'<=0.01	local stars_disadv_`i' "$^{***}$"
	else if `p_disadv_`i'_long'<=0.05	local stars_disadv_`i' "$^{**}$"
	else if `p_disadv_`i'_long'<=0.1	local stars_disadv_`i' "$^{*}$"
	else 						    local stars_disadv_`i' ""
	
	local est_gpa_`i': 	display %4.3f _b[GPA11_3_std]
	local se_gpa_`i': 		display %4.3f _se[GPA11_3_std]
	local p_gpa_`i'_long: 	display (2 * ttail(e(df_r), abs(_b[GPA11_3_std]/_se[GPA11_3_std])))
	
	if 		`p_gpa_`i'_long'<=0.01	local stars_gpa_`i' "$^{***}$"
	else if `p_gpa_`i'_long'<=0.05	local stars_gpa_`i' "$^{**}$"
	else if `p_gpa_`i'_long'<=0.1	local stars_gpa_`i' "$^{*}$"
	else 						local stars_gpa_`i' ""	

cap file close sumstat
file open sumstat using "${tableC6}", write replace
file write sumstat "\begin{tabular}{l c c c c c c c} \\ \hline\hline" _n
file write sumstat "Dependent variable & \multicolumn{3}{c}{Same study program} & & \multicolumn{3}{c}{Same study program consecutively} \\" _n
file write sumstat "                   & \multicolumn{3}{c}{consecutively}                  & & \multicolumn{3}{c}{on the top of the ROL} \\ \cline{2-4} \cline{6-8}" _n
file write sumstat " & (1) & (2) & (3) & & (4) & (5) & (6) \\ \hline" _n

local lab_var: variable label disadv

file write sumstat "`lab_var' & `est_disadv_1'`stars_disadv_1' & & `est_disadv_3'`stars_disadv_3'& & `est_disadv_4'`stars_disadv_4' & &  `est_disadv_6'`stars_disadv_6'\\ " _n
file write sumstat " & (`se_disadv_1') & &  (`se_disadv_3') & & (`se_disadv_4') & & (`se_disadv_6')  \\ " _n

local lab_var: variable label GPA11_3_std
file write sumstat "`lab_var' &  &  `est_gpa_2'`stars_gpa_2' & `est_gpa_3'`stars_gpa_3' & & & `est_gpa_5'`stars_gpa_5' & `est_gpa_6'`stars_gpa_6' \\ " _n
file write sumstat " &  & (`se_gpa_2') & (`se_gpa_3') &&&  (`se_gpa_5') &  (`se_gpa_6') \\ \hline" _n

qui sum mp if disadv == 0
local mean_mp: display %4.3f r(mean)
qui sum mp1 if disadv == 0
local mean_mp1: display %4.3f r(mean)

file write sumstat " Mean outcome (non-disadvd.) & `mean_mp' & `mean_mp' & `mean_mp' && `mean_mp1' & `mean_mp1' & `mean_mp1' \\ " _n
file write sumstat "R-squared                   & `r2_1' & `r2_2' & `r2_3' && `r2_4' & `r2_5' & `r2_6' \\ \hline\hline" _n

count
local sample_size: display %12.0fc r(N)

count if GPA11_3_std_m == 1
local missing_gpa: display %12.0fc r(N)

file write sumstat "\multicolumn{8}{p{16.5cm}}{{\it Notes}: The table presents the coefficient of linear regressions of ROL characteristics (such as whether an applicant ranked the same study program consecutively (columns (1)--(3)) and whether an applicant ranked the same study program consecutively on the top of the ROL (columns (4)--(6))) on socioeconomic status and academic achievement. The sample includes $`sample_size'$ applicants. The regressions include indicators for missing values of standardized 11th-grade GPA ($`missing_gpa'$ applicants). Robust standard errors are in parentheses.} \\ " _n
file write sumstat "\multicolumn{8}{p{16.5cm}}{***: p<0.01, **: p<0.05, *: p<0.1.}" _n
file write sumstat "\end{tabular}" _n
file close sumstat